//
//  BindCardController.m
//  WuJieLive
//
//  Created by 张瀚坤 on 2017/11/7.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "BindCardController.h"
#import "ChooseBankView.h"
#import "CheckPhoneController.h"
#import "BindBankModel.h"

@interface BindCardController ()
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIView *nameView;
@property (weak, nonatomic) IBOutlet UIButton *cardOfBankBtn;
@property (weak, nonatomic) IBOutlet UITextField *nameText;
@property (weak, nonatomic) IBOutlet UITextField *numText;
@property (nonatomic , strong) NSString * cardId;

@property (nonatomic , strong) NSMutableArray * dataArray;

@end

@implementation BindCardController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

-(void)setNavi
{
    self.title = @"绑定银行卡";
}

-(void)setUI
{
    self.nextBtn.layer.masksToBounds = YES;
    self.nextBtn.layer.cornerRadius = 20;
    self.view.backgroundColor = Color(245, 245, 245);
}

-(void)setData
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@bank-name/bank-list",BaseUrl] para:nil success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (isSuccess) {
            for (int i = 0; i < [responseObject[@"data"] count]; i ++) {
                BindBankModel * model = [[BindBankModel alloc]init];
                [model setValuesForKeysWithDictionary:[responseObject[@"data"]objectAtIndex:i]];
                [self.dataArray addObject:model];
            }
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

- (IBAction)nextAction:(id)sender {
    if (!self.cardId) {
        return;
    }else
    {
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        NSDictionary * dict = @{@"token":self.userToken,@"bank_id":@"",@"card_num":self.numText.text,@"card_user_name":self.nameText.text};
        [HTTPTool POSTForm:[NSString stringWithFormat:@"%@",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            if (isSuccess) {
                CheckPhoneController * vc = [[CheckPhoneController alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }
        } failure:^(NSError *error) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
    }
}

- (IBAction)removeViewAction:(UIButton *)sender {
    [sender removeFromSuperview];
    [self.headerView removeFromSuperview];
    CGRect frame = self.nameView.frame;
    frame.origin.y = 0;
    [UIView animateWithDuration:1 animations:^{
        self.headerView.frame = frame;
    }];
}

#pragma mark 选择银行卡操作
- (IBAction)chooseBankCard:(UIButton *)sender {
    [self.view endEditing:YES];
    ChooseBankView * view = [[[NSBundle mainBundle]loadNibNamed:@"ChoseBankView" owner:self options:nil] firstObject];
    view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    view.dataArr = self.dataArray;
    view.stringBlock = ^(BindBankModel *model) {
        self.cardId = model.bank_id;
        [self.cardOfBankBtn setTitle:model.bank_name forState:(UIControlStateNormal)];
    };
    [self.view addSubview:view];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
